package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.WhiteListUpdateDTO;
import com.xbongbong.paas.pojo.vo.ApiTokenVO;
import com.xbongbong.paas.pojo.vo.TokenUpdateVO;
import com.xbongbong.paas.pojo.vo.WhiteListUpdateVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.ApiTokenService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: api-token
 * @Author: hongxiao
 * @date: 2019-03-25 14:35
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/apiToken")
public class ApiTokenController {

    @Resource
    private ApiTokenService apiTokenService;

    /**
     * api-token中的token更新
     * @param baseDTO
     * @param br
     * @return
     * 创建时间 2019/10/10 8:13 PM
     * 修改时间 2019/10/10 8:13 PM
     * @author chy
     * @since v1.1.3
     * @version v1.1.3
     */
    @RequestMapping(value = "/tokenUpdate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String apiTokenUpdate(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<TokenUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TokenUpdateVO tokenUpdateVO = apiTokenService.tokenUpdate(baseDTO);
                response = new XbbResponse<>(tokenUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * api-token中的token更新
     * @param whiteListUpdateDTO
     * @param br
     * @return
     * 创建时间 2019/10/10 8:13 PM
     * 修改时间 2019/10/10 8:13 PM
     * @author chy
     * @since v1.1.3
     * @version v1.1.3
     */
    @RequestMapping(value = "/whiteListUpdate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String whiteListUpdate(@RequestBody @Valid WhiteListUpdateDTO whiteListUpdateDTO, BindingResult br) {
        XbbResponse<WhiteListUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WhiteListUpdateVO whiteListUpdateVO = apiTokenService.whiteListUpdate(whiteListUpdateDTO);
                response = new XbbResponse<>(whiteListUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取api-token
     * @param baseDTO
     * @param br
     * @return
     * 创建时间 2019/10/10 8:13 PM
     * 修改时间 2019/10/10 8:13 PM
     * @author chy
     * @since v1.1.3
     * @version v1.1.3
     */
    @RequestMapping(value = "/getApiToken", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getApiToken(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<ApiTokenVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ApiTokenVO apiTokenVO = apiTokenService.getApiToken(baseDTO);
                response = new XbbResponse<>(apiTokenVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
